package edu.vt.sdm.tcga.View;

import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import edu.vt.sdm.tcga.Activator;
import edu.vt.sdm.tcga.datamining.WorkerThread;

public class HeatMapExample extends ViewPart {

	private Image image;
	private HeatMapExample __thisHeatMap;
	private Label label;

	// The image width
	private static int IMAGE_WIDTH = 85;
	private static final float GRAVITY = .25f;
	private static final float FRICTION_FLOOR = 5f / 9f;
	private static final float FRICTION_WALL = 5f / 11f;
	private float x = 0;
	private float y = 0;
	private float a = 0;
	private float directionX = 4;
	private float directionY = 0;
	private float directionA = 0;
	private Canvas canvas;

	public HeatMapExample() {
		// TODO Auto-generated constructor stub
		image = Activator.getImageDescriptor("icons/Heatmap.png").createImage();
		__thisHeatMap = this;
	}

	@Override
	public void createPartControl(Composite parent) {
		// Text text = new Text(parent, SWT.BORDER);
		// text.setText("Imagine a fantastic user interface here");

		// main composite
		// ScrolledComposite top = new ScrolledComposite(parent, SWT.H_SCROLL |
		// SWT.V_SCROLL | SWT.BORDER);
		// Create the canvas for drawing
//		Composite composite = null;
//		ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL
//				| SWT.V_SCROLL | SWT.BORDER);
//		composite = new Composite(sc, SWT.NONE);
//		composite.setLayout(new FillLayout());
//		composite.setSize(400, 400);
//		sc.setContent(composite);
//		sc.setExpandHorizontal(true);
//		sc.setExpandVertical(true);
//		sc.setMinSize(composite.computeSize(image.getBounds().width, image
//				.getBounds().height));

		// Canvas canvas = new Canvas(composite, SWT.NONE);
		// canvas.addPaintListener(new PaintListener() {
		// @Override
		// public void paintControl(PaintEvent e) {
		// // TODO Auto-generated method stub
		// GC gc = e.gc;
		// gc.drawImage(image, 0, 0,
		// image.getBounds().width,
		// image.getBounds().height,
		// 20,20,
		// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getBounds().width,
		// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getBounds().height);
		//
		// }
		// });

//		Button IButton = new Button(parent, SWT.PUSH);
//		IButton.setText("Question?");
//		IButton.addSelectionListener(new SelectionAdapter() {
//
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				Shell IShell = PlatformUI.getWorkbench()
//						.getActiveWorkbenchWindow().getShell();
//				boolean isHavingFun = MessageDialog.openQuestion(IShell,
//						"Information", "Are you having fun?");
//				if (isHavingFun) {
//					MessageDialog.openInformation(IShell, "Information",
//							"You are having fun. Java is awesome!");
//				} else {
//					MessageDialog
//							.openWarning(IShell, "Warning",
//									"You are not having fun! Go for a walk and get some fresh air");
//				}
//			}
//
//		});
//
//		// Custom Action for the View's Menu
//		ViewRestoreAction ICustomAction = new ViewRestoreAction();
//		ICustomAction.setText("Restore the graphs");
//		ICustomAction.setImageDescriptor(Activator
//				.getImageDescriptor("icons/Restore16.png"));
//		this.getViewSite().getActionBars().getToolBarManager().add(
//				ICustomAction);
//
//		this.getViewSite().getActionBars().getMenuManager().add(ICustomAction);
//		this.getViewSite().getActionBars().getMenuManager()
//				.add(new Separator());
//		this.getViewSite().getActionBars().getMenuManager().add(ICustomAction);
//
//		label = new Label(parent, SWT.None);
//		WorkerThread wt = new WorkerThread(this);
//		wt.go();

//		parent.setBackground(new Color(parent.getDisplay(), 205, 38, 38));
//
//		image = Activator.getImageDescriptor("icons/Restore16.png").createImage();
//		canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
//		canvas.setBackground(new Color(parent.getDisplay(), 0, 0, 0));
//		canvas.addPaintListener(new PaintListener() {
//
//			@Override
//			public void paintControl(PaintEvent e) {
//				GC gc = e.gc;
//				Transform trans = new Transform(e.display);
//				gc.getTransform(trans);
//				trans.translate(x, y);
//				trans.translate(IMAGE_WIDTH / 2f, IMAGE_WIDTH / 2f);
//				trans.rotate(a);
//				trans.translate(-IMAGE_WIDTH / 2f, -IMAGE_WIDTH / 2f);
//				gc.setTransform(trans);
//				trans.dispose();
//
//				gc.drawImage(image, 0, 0, image.getBounds().width, image
//						.getBounds().height, 0, 0, IMAGE_WIDTH, IMAGE_WIDTH);
//
//			}
//		});
//		AnimatorThread at = new AnimatorThread();
//		at.go();
	}

	public void animate() {

		Display.getDefault().asyncExec(new Runnable() {

			public void run() {

				try {

					float left = x;
					float top = y;

					// Determine the ball's location
					directionY += GRAVITY;
					x += directionX;
					y += directionY;
					a += directionA;

					// Determine out of bounds
					Rectangle rect = canvas.getClientArea();
					if (x > rect.width - IMAGE_WIDTH) {
						x = rect.width - IMAGE_WIDTH;
						directionX = -directionX;
						directionA -= (directionY - directionA) * FRICTION_WALL;
					}
					if (x < 0) {
						x = 0;
						directionX = -directionX;
						directionA += (directionY - directionA) * FRICTION_WALL;
					}

					if (y > rect.height - IMAGE_WIDTH) {
						directionY = (int) (-GRAVITY
								* Math
										.sqrt((1 + 8
												* (rect.height - IMAGE_WIDTH)
												/ GRAVITY)) / 2);
						y = rect.height - IMAGE_WIDTH;
						directionA += (directionX - directionA)
								* FRICTION_FLOOR;
					}

					float right = left + IMAGE_WIDTH;
					float bottom = top + IMAGE_WIDTH;
					if (x < left)
						left = x;
					else
						right = x + IMAGE_WIDTH;
					if (y < top)
						top = y;
					else
						bottom = y + IMAGE_WIDTH;

					// Force a redraw
					canvas.redraw((int) Math.floor(left) - 1, (int) Math
							.floor(top) - 1, (int) (Math.ceil(right) - Math
							.floor(left)) + 2, (int) (Math.ceil(bottom) - Math
							.floor(top)) + 2, false);

				} catch (SWTException e) {
					// eat it!
				}
			}
		});

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public void updateLabelText(final String labelText) {
		try {
			Display.getDefault().asyncExec(new Runnable() {

				@Override
				public void run() {
					if (!label.isDisposed() && label != null) {
						label.setText(labelText);
					}
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class AnimatorThread implements Runnable {
		private static final int TIMER_INTERVAL = 15; // ms

		public void go() {
			Thread t = new Thread(this);
			t.start();
		}

		public void run() {
			while (true) {
				try {
					animate();
					Thread.sleep(TIMER_INTERVAL);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
